//
//  HomeManager.m
//  LOL
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 syx. All rights reserved.
//

#import "HomeManager.h"
#define ScrollURL @"http://qt.qq.com/static/pages/news/phone/c13_list_1.shtml"
#define HomeURL @"http://www.xiushuang.com/client/Portal/p_list/catid/0/p/1/game/Lol/sid/index.json"

@interface HomeManager ()

@property (nonatomic,strong) NSMutableArray *homeResourceArr;
@property (nonatomic,assign) NSInteger index;

@end

@implementation HomeManager

-(NSMutableArray *)homeResourceArr{
    if (!_homeResourceArr) {
        _homeResourceArr = [NSMutableArray new];
    }
    return _homeResourceArr;
}

-(instancetype)init{
    self = [super init];
    if (self) {
        self.index = 1;
    }
    return self;
}

static HomeManager * homeManager = nil;
+ (instancetype)homeManager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        homeManager = [[HomeManager alloc]init];
    });
    return homeManager;
}

// 解析数据获取轮播图片内容
- (void)getScrollArrComplete:(void(^)(NSArray * picArr,NSArray * titleArr,NSMutableArray * scrollArr))block{
    NSURL *url = [NSURL URLWithString:ScrollURL];
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    if (error) {
        NSLog(@"解析轮播图片出错");
        return ;
    }
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray * listArr = dic[@"list"];
        NSMutableArray * picArr = [NSMutableArray new];
        NSMutableArray * titleArr = [NSMutableArray new];
        NSMutableArray * scrollUrlArr = [NSMutableArray new];
    for (NSDictionary *dic in listArr) {
        NSString * picName = [NSString new];
        picName = [dic objectForKey:@"image_url_big"];
        
        NSString * titleStr = [NSString new];
        titleStr = [dic objectForKey:@"title"];
        
        NSString * articleStr = [NSString new];
        articleStr = [dic objectForKey:@"article_url"];
        
        [picArr addObject:picName];
        [titleArr addObject:titleStr];
        [scrollUrlArr addObject:articleStr];
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        block(picArr,titleArr,scrollUrlArr);
    });
}];
    [dataTask resume];

}


// 解析数据获取主页内容
- (void)getHomeCompletionHandle:(void(^)())handle{
    if (self.UPDrow == YES) {
        self.index = self.index + 1;
        NSString * urlStr = [NSString stringWithFormat:@"http://www.xiushuang.com/client/Portal/p_list/catid/0/p/%ld/game/Lol/sid/index.json",self.index];
        NSURL *url = [NSURL URLWithString:urlStr];
        NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession]dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            if (error) {
                NSLog(@"解析主页数据出错");
                return ;
            }
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSArray * articleArr = dic[@"article"];
            for (NSDictionary * dic in articleArr) {
                HomeTableModel * homeModel = [HomeTableModel new];
                [homeModel setValuesForKeysWithDictionary:dic];
                [self.homeResourceArr addObject:homeModel];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                handle();
            });
        }];
        [dataTask resume];
    }else{
    NSURL *url = [NSURL URLWithString:HomeURL];
    NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession]dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
            NSLog(@"解析主页数据出错");
            return ;
        }
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray * articleArr = dic[@"article"];
        for (NSDictionary * dic in articleArr) {
            HomeTableModel * homeModel = [HomeTableModel new];
            [homeModel setValuesForKeysWithDictionary:dic];
            [self.homeResourceArr addObject:homeModel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            handle();
        });
    }];
    [dataTask resume];
    }
}

// 返回数据数量
- (NSUInteger)getHomeResourceCount{
    return self.homeResourceArr.count;
}

// 返回模型
- (HomeTableModel *)getHomeModelWithIndex:(NSUInteger)index{
    return self.homeResourceArr[index];
}





@end
